<template>
	<view>
		<u-navbar
			leftIconSize="32rpx" 
			:placeholder="true" 
			:fixed="true" 
			:safeAreaInsetTop="true" 
			:autoBack="true"
			bgColor="#fff"
			:border-bottom="false">
				<view slot="left">
					<u-icon size="42" name="arrow-left" color="#333"></u-icon>
				</view>
				<view slot="center" style="display: flex;justify-content: center;color: #fff;">
					<view class="index-search" @click="openPage('pages/product/search', {type: type})">
						<u-icon name="search" color="#f5f5f5" size="30"></u-icon>
						<text style="color: #f5f5f5;font-size: 24rpx;margin-left: 10rpx;letter-spacing: 2rpx;">{{params.keyword?params.keyword:'搜索你想要的商品'}}</text>
					</view>
				</view>
			</u-navbar>

		<view class="sortContent">
			<view :class="{ active: sort === 1 }" @click="sortFn(1)">
				<text class="sortText">综合</text>
			</view>
			<view :class="{ active: sort === 2 }" @click="sortFn(2)">
				<text class="sortText">销量</text>
			</view>
			<view :class="{ active: sort === 3 }" class="sortPriceContent" @click="sortFn(3)">
				<text class="sortText">价格</text>
				<view class="sortPriceUptoDown">
					<u-icon size="20" v-if="sort == 3 && sortPrice==1" color="#FF375C" name="arrow-up-fill"></u-icon>
					<u-icon size="20" v-else color="#333" name="arrow-up-fill"></u-icon>
					<u-icon size="20" v-if="sort == 3 && sortPrice==2" color="#FF375C" name="arrow-down-fill"></u-icon>
					<u-icon size="20" v-else color="#333" name="arrow-down-fill"></u-icon>
				</view>
			</view>
		</view>
		<view style="margin: 0 20rpx; margin-top: 20rpx;border-radius: 20rpx;overflow: hidden;">
			<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback"
				:down="downOption" :up="upOption">
				<view style="display: flex;flex-direction: row;flex-wrap: wrap;justify-content: space-between;">
					<view style="width: 348rpx;background-color: #fff;margin-bottom: 20rpx;" v-for="(item,index) in list" :key="index"
						@click="openPage('pages/product/goodsDetail', {id: item.id, type: item.type})">
						<goodsItem :goods="item"></goodsItem>
					</view>
				</view>
				<!-- <view class="wrap">
					<u-waterfall v-model="list" ref="uWaterfall">
						<template v-slot:left="{leftList}">
							<view class="warter" v-for="(item, index) in leftList" :key="index">
								<view @click="openPage('pages/index/selfGoodsDetail', {id: item.id, type: item.type})">
									<goodsItem :goods="item"></goodsItem>
								</view>
							</view>
						</template>
						<template v-slot:right="{rightList}">
							<view class="warter" v-for="(item, index) in rightList" :key="index">
								<view @click="openPage('pages/index/selfGoodsDetail', {id: item.id, type: item.type})">
									<goodsItem :goods="item"></goodsItem>
								</view>
							</view>
						</template>
					</u-waterfall>
				</view> -->
			</mescroll-body>
		</view>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import ptList from "../../components/ptList.vue"
	import goodsItem from "../../components/waterfallGoodsItem.vue"
	import imagesUrl from '../../common/images.js'
	import {goodsList} from "@/common/shopmall.api.js"
	
	export default {
		components: {
			ptList,
			goodsItem
		},
		mixins: [MescrollMixin],
		data() {
			return {
				background: {
					// backgroundImage: 'linear-gradient(to left, rgba(255, 0, 60, 1) , #FF375C)'
					background: '#fff'
				},
				nav: [],
				swiper: [],
				current: 0,
				pagination: {
					auto: false,
					page: 1,
					pageSize: 20,
					total: 0,
				},
				loadStatus: 'loadmore',
				upOption: {
					auto: false,
					page: {
						size: 20 // 每页数据的数量,默认10
					},
					empty: {
						tip: '暂无相关数据'
					},
					textNoMore: "没有更多数据了"
				},
				list: [],
				classify: [],
				classifyId: 0,
				type: 5,
				activity: [],
				sort: 1,
				sortPrice: 1,
				params:null
			}
		},
		onLoad(params) {
			this.params = params
		},
		methods: {
			openPage(path, params) {
				this.$u.route({
					url: path,
					params
				})
			},
			getClassify() {
				let that = this;
				this.$u.api.classify({
					type: this.type
				}).then(res => {
					that.classify = res
				})
			},
			getData(page) {
				let that = this;
				let params = {
					page,
					pageSize: this.pagination.pageSize,
					type: this.type,
					classify_id: this.classifyId,
					...this.params
				}
				
				if(this.sort == 2){
					params.sort = 'sales'
					params.sort_mode = 'desc'
				}else if(this.sort == 3){
					params.sort = 'price'
					if(this.sortPrice == 1){
						params.sort_mode = 'asc'
					}else{
						params.sort_mode = 'desc'
					}
				}
				goodsList(params).then(res => {
					that.mescroll.endByPage(res.data.length, res.pagination.total)
					that.pagination = res.pagination;
					if (page == 1) {
						that.list = res.data
					} else {
						that.list = [...that.list, ...res.data]
					}
					that.pagination.page++
					
				})
				
			},
			upCallback() {
				this.getData(this.pagination.page)
			},
			downCallback() {
				this.pagination.page = 1;
				this.mescroll.resetUpScroll();
			},
			
			sortFn(data){
				this.sort = data
				if(data == 3){
					if(this.sortPrice == 1){
						this.sortPrice = 2
					}else{
						this.sortPrice = 1
					}
				}
				this.getData(1)
			}
		}
	}
</script>

<style>
	/* page不能写带scope的style标签中，否则无效 */
	page {
		background-color: rgb(240, 240, 240);
	}
</style>

<style lang="scss" scoped>
	.index-search {
		width: 630rpx;
		height: 54rpx;
		background-color: #ccc;
		border-radius: 60rpx;
		display: flex;
		align-items: center;
		padding-left: 24rpx;
		margin-left: 60rpx;
	}

	.warter {
		border-radius: 8px;
		margin: 5px;
		background-color: #ffffff;
		position: relative;
	}

	.sortContent {
		display: flex;
		background-color: #fff;
		flex-direction: row;
		justify-content: space-between;
		padding: 20rpx 40rpx;

		.sortPriceContent {
			display: flex;
			flex-direction: row;
			align-items: center;

			.sortPriceUptoDown {
				display: flex;
				flex-direction: column;
				margin-left: 10rpx;
			}
		}

		.sortText {
			font-size: 32rpx;
			color: #333;
		}
		
		.active{
			.sortText{
				color: #FF375C;
			}
		}
	}
</style>
